In [1]:
import numpy as np
import matplotlib.pyplot as plt
In [2]:
%matplotlib inline
plt.rc("figure", figsize=(10,6))

On rappellera l'utilisation de la combinaison shift+tab après une fonction pour voir rapidement les options disponibles

Loi Binomiale

In [3]:
valeurs = np.random.binomial(30, 0.5, size=(50))
valeurs
Out[3]:
array([19, 15, 14, 13, 20, 15,  9, 20, 18, 14, 16, 13, 11, 17, 14, 11, 16,
       15, 14, 19, 17, 19, 18, 12, 15, 15, 16, 15, 16, 14, 17, 20, 18, 17,
       12, 15, 15,  8, 18, 21, 14, 13, 17, 17, 20, 13, 14, 15, 16, 17])

Attention pour obtenir les fréquences on utilisera la syntaxe suivante

In [4]:
abcisses, hauteur = np.unique(valeurs, return_counts=True)
plt.bar(abcisses, hauteur)
Out[4]:
<Container object of 13 artists>

Loi Uniforme

Réels entre 0 et 1

In [5]:
valeurs = np.random.rand(5,6)
valeurs
Out[5]:
array([[ 0.93850128,  0.18796392,  0.37865753,  0.64244049,  0.44601222,
         0.10272039],
       [ 0.47396528,  0.87546649,  0.25431498,  0.56618553,  0.5551907 ,
         0.95649703],
       [ 0.06446791,  0.83413468,  0.66846618,  0.07198018,  0.7942323 ,
         0.03978156],
       [ 0.65865506,  0.81875555,  0.57030029,  0.95289563,  0.94730548,
         0.43713166],
       [ 0.52279956,  0.93615673,  0.97364778,  0.45149953,  0.21552192,
         0.70556249]])
In [6]:
valeurs = np.random.random(size=(10, 10))
valeurs
Out[6]:
array([[ 0.29220573,  0.31079192,  0.33056074,  0.80402969,  0.95871391,
         0.06779476,  0.72618233,  0.60256657,  0.36620845,  0.78124706],
       [ 0.51109507,  0.3656647 ,  0.26048209,  0.36322291,  0.44623542,
         0.85275595,  0.70288951,  0.30791714,  0.42389332,  0.29471026],
       [ 0.67833804,  0.45984061,  0.63228031,  0.09483863,  0.73457366,
         0.34676863,  0.49151663,  0.41049183,  0.94920027,  0.49208565],
       [ 0.52450493,  0.89564584,  0.752529  ,  0.48117346,  0.12238883,
         0.26602871,  0.41601298,  0.2806451 ,  0.46037733,  0.71840041],
       [ 0.78521371,  0.4763424 ,  0.60864779,  0.38421241,  0.37108375,
         0.41245364,  0.07457419,  0.41192982,  0.18390829,  0.76739349],
       [ 0.29251458,  0.79456066,  0.53845913,  0.38195163,  0.48843095,
         0.03257068,  0.16813476,  0.80288264,  0.70925839,  0.63567889],
       [ 0.99637249,  0.92991527,  0.74616779,  0.52096469,  0.33833945,
         0.25375972,  0.12494955,  0.23886472,  0.43596301,  0.6388692 ],
       [ 0.53432153,  0.7166131 ,  0.91914358,  0.14828653,  0.29420762,
         0.7996343 ,  0.96542959,  0.25318054,  0.11806101,  0.46362725],
       [ 0.67315851,  0.37590313,  0.23212098,  0.69326345,  0.08171893,
         0.05141434,  0.10053517,  0.95722003,  0.51047609,  0.04611967],
       [ 0.26371469,  0.57514721,  0.72296217,  0.03897916,  0.28078845,
         0.16123482,  0.03110692,  0.57663138,  0.4377996 ,  0.67042687]])

Pour les histogrammes on fera attention à l'option normed pour normaliser le résultat.

In [7]:
plt.hist(valeurs.flatten(), normed=True)
Out[7]:
(array([ 1.34677963,  0.41439373,  1.55397649,  1.45037806,  1.45037806,
         0.82878746,  0.82878746,  1.45037806,  0.3107953 ,  0.72518903]),
 array([ 0.03110692,  0.12763348,  0.22416003,  0.32068659,  0.41721315,
         0.5137397 ,  0.61026626,  0.70679282,  0.80331937,  0.89984593,
         0.99637249]),
 <a list of 10 Patch objects>)
In [8]:
plt.hist(valeurs.flatten(), cumulative=True)
Out[8]:
(array([  13.,   17.,   32.,   46.,   60.,   68.,   76.,   90.,   93.,  100.]),
 array([ 0.03110692,  0.12763348,  0.22416003,  0.32068659,  0.41721315,
         0.5137397 ,  0.61026626,  0.70679282,  0.80331937,  0.89984593,
         0.99637249]),
 <a list of 10 Patch objects>)

Réels entre 2 valeurs à spécifiées

In [9]:
valeurs = np.random.uniform(-1, 5, size=(200))
In [10]:
plt.hist(valeurs, normed=True)
Out[10]:
(array([ 0.1972013 ,  0.18005336,  0.14575748,  0.22292321,  0.20577527,
         0.13718351,  0.10288764,  0.1972013 ,  0.1972013 ,  0.12860955]),
 array([-0.95344737, -0.37028692,  0.21287353,  0.79603398,  1.37919442,
         1.96235487,  2.54551532,  3.12867577,  3.71183622,  4.29499666,
         4.87815711]),
 <a list of 10 Patch objects>)
In [11]:
plt.hist(valeurs, cumulative=True, normed=True)
Out[11]:
(array([ 0.115,  0.22 ,  0.305,  0.435,  0.555,  0.635,  0.695,  0.81 ,
         0.925,  1.   ]),
 array([-0.95344737, -0.37028692,  0.21287353,  0.79603398,  1.37919442,
         1.96235487,  2.54551532,  3.12867577,  3.71183622,  4.29499666,
         4.87815711]),
 <a list of 10 Patch objects>)

Entiers entre 2 valeurs

In [12]:
valeurs = np.random.randint(-5, 6, size=(5,5))
valeurs
Out[12]:
array([[ 3, -4,  4,  0, -2],
       [ 5,  4,  5,  4,  0],
       [ 5, -1, -3, -3,  2],
       [ 3, -2,  5, -4,  3],
       [-3,  0, -3,  1, -5]])

Attention la plus haute valeur indiquée est en fait exclue

Echantillon d'un tableau

In [13]:
valeurs = np.sin(np.linspace(-np.pi, np.pi, 50))
choix = np.random.choice(valeurs, size=(30), replace=True)
In [14]:
abc, haut = np.unique(choix, return_counts=True)
plt.bar(abc, haut, width=np.pi/100)
Out[14]:
<Container object of 22 artists>

Mélange

Nouveau tableau

In [15]:
valeurs = np.arange(10)
valeurs
Out[15]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [16]:
np.random.permutation(valeurs)
Out[16]:
array([5, 4, 8, 3, 1, 6, 2, 9, 0, 7])
In [17]:
valeurs
Out[17]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

Sur le tableau

In [18]:
valeurs = np.random.randint(-2, 3, size=(15))
valeurs
Out[18]:
array([ 1,  2,  2, -2,  0, -1,  2, -1,  1, -1,  0, -1,  2, -2,  2])
In [19]:
np.unique(valeurs, return_counts=True)
Out[19]:
(array([-2, -1,  0,  1,  2]), array([2, 4, 2, 2, 5]))
In [20]:
np.random.shuffle(valeurs)
valeurs
Out[20]:
array([ 2, -2,  1, -1,  2, -1,  0,  2, -2, -1, -1,  1,  2,  2,  0])
In [21]:
np.unique(valeurs, return_counts=True)
Out[21]:
(array([-2, -1,  0,  1,  2]), array([2, 4, 2, 2, 5]))

Gaussienne

In [22]:
valeurs = np.random.normal(-5, 12, size=(200))
In [23]:
_ = plt.hist(valeurs)

On va plutôt utiliser la libraire seaborn pour un affichage plus représentatif via une estimation par noyau.

In [24]:
import seaborn as sns
In [25]:
sns.kdeplot(valeurs)
Out[25]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fd39d49c470>

#

In [26]:
valeurs = np.random.poisson(4.0, size=(400))
abc, haut = np.unique(valeurs, return_counts=True)
plt.bar(abc, haut)
Out[26]:
<Container object of 11 artists>
In [27]:
sns.kdeplot(valeurs)
Out[27]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fd39d3ccf28>

Loi exponentielle

In [28]:
valeurs = np.random.exponential(1.0, size=(100))
sns.kdeplot(valeurs)
Out[28]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fd39d313908>

On pourra explorer le module pour bien d'autres lois